// pages/start/start.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo: {},
    angle: 0,
    status: false, //是否通过审核
    remind: '加载中',
    checkUser: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  /** 
  onLoad: function (options) {
    let that = this;
    let uid = wx.getStorageSync('uid');
    if(uid == ''){
      console.log('1111111111==');
      uid = app.globalData.uid
      setTimeout(function () {
        that.setData({
          remind: ''
        });
      }, 1500);
    }else{
      console.log('1111111111==');
     
     that.setData({
      userInfo: wx.getStorageSync('userInfo'),
      remind: ''
    })
    }
    
    this.setData({
      uid:uid
    })
  },
*/
onLoad: function(t) {
  var o = this, e = wx.getStorageSync("uid");
  "" == e ? (e = app.globalData.uid, setTimeout(function() {
      o.setData({
          remind: ""
      });
  }, 1500)
  ) : wx.request({
      url: app.globalData.url +  app.interfaceAddress.getWxUserInfo,
      method: "get",
      dataType: "json",
      data: {
          uid: e
      },
      success: function(t) {
          console.log(t), o.setData({
              userInfo: t.data.data,
              remind: ""
          }), wx.setStorageSync("userInfo", t.data.data), app.globalData.uid = t.data.uid,
          console.log(app.globalData.uid);
      }
  }), this.setData({
      uid: e
  });
},
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;
  },

  //获取用户信息并授权
  bindgetuserinfo:function(e){
    wx.showLoading({
      title: '授权中',
    })
    var that = this;
    wx.getUserInfo({
      lang: 'zh_CN',
      success: function (res) {
        var userInfo = res.userInfo
        wx.login({
          success: function (res) {
            if (res.code) {
              userInfo.code = res.code;
              userInfo.spid = app.globalData.spid;
              wx.request({
                url: app.globalData.url + app.interfaceAddress.getWxUserInfo,
                method: 'post',
                dataType: 'json',
                data: {
                  info: userInfo
                },
                success: function (res) {
                  wx.hideLoading();
                  console.log(res)
                  wx.setStorageSync('uid', res.data.data.uid);
                  app.globalData.uid = res.data.data.uid;
                  app.globalData.openid = res.data.data.routine_openid;
                  that.setData({
                    userInfo:res.data.data
                  });
                }
              })
            }else{
              console.log('登录失败！' + res.errMsg)
              wx.hideLoading();
            }
          }
        });
      }
    })
  },

  /** 
  goSign:function(){
    var that = this;
    wx.showLoading({
      title: '正在加载',
    });
    //var uid = app.globalData.uid;
    let uid = wx.getStorageSync('uid');
        console.log("+++++uid++++++===="+uid);
    // wx.request({
    //   url: app.globalData.url + '/routine/auth_api/get_setting_value',
    //   method: 'get',
    //   dataType: 'json',
    //   data: {
    //     uid: uid,
    //     key: 'checkUser'
    //   },
    //   success: function (res) {
    //     let checkUser = false;
    //     if (res.data.data.value == "true")
    //       checkUser = true
    //     if (checkUser) {
    //       let userInfo = that.data.userInfo;
    //       console.log(userInfo)
    //       if (userInfo.status == 2) {
    //         that.startInit();
    //       } else if (userInfo.status == 1) {
    //         wx.navigateTo({
    //           url: '../status/status',
    //         })
    //       } else {
    //         wx.navigateTo({
    //           url: '../register/register',
    //         })
    //       }
    //       wx.hideLoading()
    //     } else {
    //       that.startInit();
    //       wx.hideLoading();
    //     }
    //   }
    // })
    if(uid == null || uid ==''){
      console.log("uid22222=");
      var that = this;
      wx.getUserInfo({
        lang: 'zh_CN',
        desc: '用于完善会员资料',
        success: function (res) {
          wx.showLoading({
            title: '授权中',
          })
          var userInfo = res.userInfo
          wx.login({
            success: function (res) {
              if (res.code) {
                console.log('aaaaa='+res.code);
                userInfo.code = res.code;
                userInfo.spid = app.globalData.spid;
                console.log('ddddd='+app.globalData.url + app.interfaceAddress.getWxUserInfo);
                wx.request({
                  //url: app.globalData.url + '/routine/login/index',
                  url: app.globalData.url + app.interfaceAddress.getWxUserInfo,
                  method: 'post',
                  dataType: 'json',
                  data: {
                    info: userInfo
                  },
                  success: function (res) {
                    console.log('sssssss');
                    wx.hideLoading();
                    console.log(res)
                    wx.setStorageSync('uid', res.data.data.uid);
                    app.globalData.uid = res.data.data.uid;
                    app.globalData.openid = res.data.data.routine_openid;
                    wx.setStorageSync('userInfo', res.data.data);
                    that.setData({
                      userInfo: res.data.data
                    });
                    that.startInit();
                    wx.hideLoading();
                  }
                })
              } else {
                console.log('登录失败！' + res.errMsg)
                wx.hideLoading();
              }
            }
          });
        }
      })
    }else {
      console.log('=====else=');
      that.setData({
        userInfo: wx.getStorageSync('userInfo')
      })
      that.startInit();
      wx.hideLoading();
    }


    //that.startInit();
    //wx.hideLoading();
  },
  **/

 goSign: function() {
    var that = this;
    wx.showLoading({
        title: "正在加载"
    });
          var userInfo = {};
          wx.login({
            success: function (res) {
              if (res.code) {
                userInfo.code = res.code;
                console.log('aaaaa='+res.code);
                wx.request({
                  url: app.globalData.url + app.interfaceAddress.getWxUserInfo,
                  method: 'post',
                  dataType: 'json',
                  data: {
                    info: userInfo
                  },
                  success: function (res) {
                    wx.hideLoading();
                    console.log(res)
                    wx.setStorageSync('uid', res.data.data.uid);
                    app.globalData.uid = res.data.data.uid;
                    app.globalData.openid = res.data.data.routine_openid;
                    wx.setStorageSync('userInfo', res.data.data);
                    that.setData({
                      userInfo: res.data.data
                    });
                   
                  }
                })
              } else {
                console.log('登录失败！' + res.errMsg)
                wx.hideLoading();
              }
            }
          });
    that.startInit(), wx.hideLoading();
},

  startInit(){
    var category = wx.getStorageSync('category');
    if(category){
      var uid = wx.getStorageSync('uid');
      getApp().SyncDataFromDB(uid,category.id)
      wx.switchTab({
        url: "../index/index"
      })
    }
    else
      wx.redirectTo({
        url: '/pages/category/category',
      })
  }

})